package com.zadtek.shopmall.activities;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.zadtek.shopmall.R;

import java.util.LinkedList;
import java.util.List;

public class GuideActivity extends AppCompatActivity {

    private ViewPager guidePage ;
    private Button btnEnter ;
    private LinearLayout layIndicator ;

    private List<View> viewList;
    private int GUIDE_PAGE_COUNTS = 2;
    private int[] imgResArr = new int[]{R.drawable.guide_1, R.drawable.guide_2};
    private List<ImageView> indicatorList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);
        initData();
        initView();
        initLogic();
    }

    private void initData() {
        viewList = new LinkedList<>();
        indicatorList = new LinkedList<>();
    }

    private void initLogic() {
        btnEnter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(GuideActivity.this,MainActivity.class);
                startActivity(intent);
                finish();
            }
        });
        guidePage.setAdapter(new GuidePageAdpter(viewList));
        guidePage.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                setIndicator(position);
                if(position==1){
                    btnEnter.setVisibility(View.VISIBLE);
                }else{
                    btnEnter.setVisibility(View.INVISIBLE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void initView() {
        guidePage = (ViewPager) findViewById(R.id.guide_page);
        btnEnter = (Button) findViewById(R.id.btn_enter);
        layIndicator = (LinearLayout) findViewById(R.id.lay_indicator);
        for (int i = 0; i <imgResArr.length ; i++) {
            ImageView view = new ImageView(this);
            view.setBackgroundResource(imgResArr[i]);
            viewList.add(view);

            ImageView view2 = new ImageView(this);
            if(i==0){
                view2.setBackgroundResource(R.drawable.selected);
            }else{
                view2.setBackgroundResource(R.drawable.unselected);
                LinearLayout.LayoutParams pa = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                pa.setMargins(20,0,0,0);
                view2.setLayoutParams(pa);
            }
            indicatorList.add(view2);
            layIndicator.addView(view2);
        }
    }

    private void setIndicator(int targetIndex) {
        for (int i = 0; i < indicatorList.size(); i++) {
            if (targetIndex == i) {
                indicatorList.get(i).setBackgroundResource(R.drawable.selected);
            }else{
                indicatorList.get(i).setBackgroundResource(R.drawable.unselected);
            }
        }
    }

    private class GuidePageAdpter extends PagerAdapter {

        private List<View> _viewList ;

        public GuidePageAdpter(List<View> viewList) {
            this._viewList = viewList;
        }

        @Override
        public int getCount() {
            return _viewList.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(_viewList.get(position));
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(_viewList.get(position));
            return _viewList.get(position);
        }
    }
}
